//
//  XTGeomancyViewController.m
//  BeautifulHome
//
//  Created by dlios on 15-5-13.
//  Copyright (c) 2015年 ACE. All rights reserved.
//

#import "XTGeomancyViewController.h"
#import "XTGeomancyTableViewCell.h"
#import "XTGeomancyDetailViewController.h"
#import "NetworkHandle.h"
#import "XTGeomancy.h"
#import "UIImageView+WebCache.h"
#import "XTGeomancyDetailViewController.h"
#import "XTMyLoginViewController.h"
#import "AppDelegate.h"
// 下拉刷新
#import "MJRefresh.h"

@interface XTGeomancyViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) NSArray *arr;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *arr1;
@property (nonatomic, retain) NSArray *arr2;
@property (nonatomic, retain) NSArray *arr3;
@end

@implementation XTGeomancyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"我的" style:UIBarButtonItemStylePlain target:self action:@selector(leftAction)];
    self.navigationItem.title = @"装修风水";
    self.arr1 = [NSMutableArray array];

    self.arr = @[@"客厅", @"卧室", @"厨房", @"卫生间"];
    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(self.view.frame.size.width / 4 - 10 , 100);
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 20, 5);
    UICollectionView *collectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50) collectionViewLayout:flowLayout];
    collectionView.dataSource=self;
    collectionView.delegate=self;
//    collectionView.backgroundColor = [UIColor redColor];
    collectionView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"9999"]];
    collectionView.backgroundView.layer.masksToBounds = YES;
    [self.view addSubview:collectionView];
    [collectionView release];
    [flowLayout release];
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self collectionView:collectionView didSelectItemAtIndexPath:0];
    [self createTableView];
    self.isUpLoading = NO;
    [self addHeader];
    [self addFooter];
    //[self creatNotificationCenter];
}
- (void)leftAction
{
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    if (tempAppDelegate.LeftSlideVC.closed)
    {
        [tempAppDelegate.LeftSlideVC openLeftView];
    }
    else
    {
        [tempAppDelegate.LeftSlideVC closeLeftView];
    }
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 4;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    cell.backgroundView.layer.masksToBounds = YES;
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 40,(self.view.frame.size.width - 100) / 5, 40)];
    label.textColor = [UIColor grayColor];
    label.backgroundColor = [UIColor clearColor];
    label.text = [NSString stringWithFormat:@"%@",_arr[indexPath.row]];
    for (id subView in cell.contentView.subviews) {
        [subView removeFromSuperview];
    }
    [cell.contentView addSubview:label];
    
    return cell;
}
//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.index = indexPath.row + 5;
    [self addHeader];
}
- (void)getDataTid:(NSInteger)tid p:(NSInteger)p
{
    NSString *url = [NSString stringWithFormat:@"http://www.to8to.com/mobileapp/xnjz/index.php?controller=fengshui&action=list&tid=%ld&p=%ld&per_page=15", tid, p];
    [NetworkHandle getDataWithURL:url completion:^(id result) {
        // result为请求接口的返回数据
        NSLog(@"%@", result);
        self.nextCursor = self.nextCursor + 1;
        if (self.isUpLoading == NO) {
            //说明是下拉刷新，就要清空数组中的数据，保持数组中存放的是最新的数据
            [self.arr1 removeAllObjects];
        }
        NSArray *arr = [[result objectForKey:@"content"] objectForKey:@"result"];
        for (NSDictionary *dic in arr) {
            XTGeomancy *geomancy = [[XTGeomancy alloc] initWithDic:dic];
            [_arr1 addObject:geomancy];
            [geomancy release];
        }
        //NSLog(@"___%@",_arr1);
        [self.tableView footerEndRefreshing];//结束底部刷新效果
        [self.tableView headerEndRefreshing];//结束头部刷新效果
        [self.tableView reloadData];
    }];
}
- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height - 49 - 64 - 50) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 80;
    [self.view addSubview:_tableView];
    [_tableView release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _arr1.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuseID = @"reuse";
    XTGeomancyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID];
    if (!cell) {
        cell = [[XTGeomancyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseID];
    }
    XTGeomancy *geo =[[XTGeomancy alloc] init];
    geo = _arr1[indexPath.row];
    cell.l1.text = geo.title;
    cell.l2.text = geo.summary;
    [cell.mImageView sd_setImageWithURL:[NSURL URLWithString:geo.image]placeholderImage:[UIImage imageNamed:@"3-1"]];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 取消选中阴影效果
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    XTGeomancyDetailViewController *gdVC = [[[XTGeomancyDetailViewController alloc] init] autorelease];
    XTGeomancy *s1 = _arr1[indexPath.row];
    gdVC.geomancy = s1;
    [self.navigationController pushViewController:gdVC animated:YES];
}
#pragma mark --下拉刷新
- (void)addHeader
{
    __block XTGeomancyViewController *gVC = self;
    // 添加下拉刷新头部控件
    [self.tableView addHeaderWithCallback:^{
        // 进入刷新状态就会回调这个Block
        gVC.isUpLoading = NO;//标记为下拉操作
        [gVC getDataTid:self.index p:1];//重新请求数据
        
    }];
    
#pragma mark 自动刷新(一进入程序就下拉刷新)
    [self.tableView headerBeginRefreshing];
    
}
#pragma mark --上拉加载更多
- (void)addFooter
{
    //    __unsafe_unretained typeof(self) vc = self;
    __block XTGeomancyViewController *gVC = self;
    
    // 添加上拉刷新尾部控件
    [self.tableView addFooterWithCallback:^{
        // 进入刷新状态就会回调这个Block
        
        gVC.isUpLoading = YES;//标记为上拉操作
        [gVC getDataTid:self.index p:self.nextCursor];//请求更多数据
        
    }];
}
- (void)creatNotificationCenter
{
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(changeColor:) name:@"color" object:nil];
}
- (void)changeColor:(NSNotification *)noti
{
    self.navigationController.navigationBar.translucent = NO;
    self.backgroundColor = noti.object;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:self.backgroundColor] forBarMetrics:UIBarMetricsDefault];
}
//- (void)viewWillDisappear:(BOOL)animated
//{
//    [super viewWillDisappear:animated];
//    //    NSLog(@"viewWillDisappear");
//    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
//    [tempAppDelegate.LeftSlideVC setPanEnabled:NO];
//}
//
//- (void)viewWillAppear:(BOOL)animated
//{
//    [super viewWillAppear:animated];
//    //    NSLog(@"viewWillAppear");
//    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
//    [tempAppDelegate.LeftSlideVC setPanEnabled:YES];
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
